package com.bizfty.apiexecutor;

import com.bizfty.apiexecutor.classloader.ExecutorClassLoader;
import javax.servlet.MultipartConfigElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.servlet.DispatcherServlet;

@SpringBootApplication
@ImportResource({"classpath*:integration.xml"})
@ComponentScan({"com.bizfty.apiexecutor"})
public class Application {

    public static String deployPath = "/web_data/deploy/application/integration/";

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static ExecutorClassLoader executorClassLoader;

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        ConfigurableEnvironment environment = new StandardEnvironment();
        application.setEnvironment(environment);
        String path = environment.getProperty("deploy.path");
        if (path == null) {
            path = Application.deployPath;
        }
        executorClassLoader = new ExecutorClassLoader(path);
        ResourceLoader rl = new DefaultResourceLoader(executorClassLoader);
        application.setResourceLoader(rl);
        application.run(args);
    }
    
    @Bean
    public ServletRegistrationBean dispatcherDefaultRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.getUrlMappings().clear();
        registration.addUrlMappings("/api/*");
        return registration;
    }

}
